<?php
##############################################
#
#   首页
# @ Time : 2017-1-25 - 下午10:07:21
# @ Author : JiangYongWei <1085562810@qq.com>
# @ Encoding : UTF-8 language : PHP
#
##############################################

namespace Home\Controller;
use Think\Controller;
use Home\Model\CategoryModel;
use Home\Model\LocalityModel;
use Home\Model\GoodsModel;

class IndexController extends Controller{
	
	private $cid;		//分类主键
	private $lid;		//地区主键
	private $url;		//检索的url
	private $price;		//价格筛选
	private $goods_db;	//商品表对象
	private $order;		//排序规则
	
	public function _initialize(){
		$this -> goods_db = new GoodsModel();
		$this -> cid = I('get.cid') ? I('get.cid') : null;
		$this -> lid = I('get.lid') ? I('get.lid') : null;
		//追加url
		$path_info = $_SERVER['PATH_INFO'] ? $_SERVER['PATH_INFO'] : 'Index/index';
	    $this -> url = 'http://'.$_SERVER['HTTP_HOST'].'/meituan/Home/'.$path_info;
	    $this -> price = I('get.price') ? I('get.price') : null;
	    $this -> order = I('get.order') ? I('get.order') : 't-desc';
	    $this -> setNav();
	    $this -> assign('username', $_SESSION['username']);
	}
	
	protected function setNav(){
		$category_model = new CategoryModel();
		$nav = $category_model -> getCategoryLevel(0);
		$this -> assign('nav', $nav);
	}
	
	/**
	 * 显示首页
	 */
    public function index(){
    	$this -> setCategory();	//设置分类
    	$this -> setLocality();	//设置地区
    	$this -> setPrice();   	//设置价格
    	$this -> setOrderUrl();
    	$this -> setOrder();
    	$this -> setSearchWhere(); //设置商品筛选条件
    	$data = $this -> goods_db -> getGoods(); //获取商品
    	$this -> assign('goods', $data[0]);
    	$this -> assign('page', $data[1]);
       	$this -> display('index');
    }
    
    /**
     * 设置分类的模板
     * 1 没有cid 显示顶级分类
     * 2 有cid 显示顶级分类与子类
     */
    private function setCategory(){
    	$url = url_param_remove('cid',$this -> url);
    	$category_model = new CategoryModel();
    	if(is_null($this -> cid)){
    		$topCategory = $category_model -> getCategoryLevel(0);
    		$tmpArr = array();
    		$tmpArr[] =  '<a class="active" href="'.$url.'">全部</a>';
    		foreach ($topCategory as $v){
    			$tmpArr[] = '<a href="'.$url.'/cid/'.$v['cid'].'">'.$v['cname'].'</a>';
    		}
    		$this->assign('topCategory',$tmpArr);
    		return;
    	}
    	//有cid的情况
     	/** 
    	 * 	1. cid 是顶级的分类id => pid = 0
    	 * 	2. cid 不是顶级分类的id
    	 */
    	$pid = $category_model -> getCategoryPid($this -> cid);
    	$topCategory = $category_model -> getCategoryLevel(0);
    	$tmpArr = array();
    	$tmpArr[] =  '<a href="'.$url.'">全部</a>';
    	foreach ($topCategory as $v){
    			if($pid == $v['cid'] || $this -> cid == $v['cid']){
    				$tmpArr[] = '<a  class="active" href="'.$url.'/cid/'.$v['cid'].'">'.$v['cname'].'</a>';
    			}else{
    				$tmpArr[] = '<a href="'.$url.'/cid/'.$v['cid'].'">'.$v['cname'].'</a>';
    			}
    	}
    	$this->assign('topCategory',$tmpArr);
    	//查出子分类
    	if($pid == 0){
    		$sonCategory = $category_model -> getCategoryLevel($this->cid);
    	}else{
    		$sonCategory = $category_model -> getCategoryLevel($pid);
    	}
    	if(is_null($sonCategory)) return;
    	//组合子分类模板
    	$tmpArr = array();
    	if($pid == 0){
    		$tmpArr[] = '<a class="active"  href="'.$url.'/cid/'.$this -> cid.'">全部</a>';
    	}else{
    		$tmpArr[] = '<a  href="'.$url.'/cid/'.$pid.'">全部</a>';
    	}
    	foreach ($sonCategory as $v){
    		if($v['cid'] == $this -> cid){
    			$tmpArr[] = '<a class="active" href="'.$url.'/cid/'.$v['cid'].'">'.$v['cname'].'</a>';
    		}else{
    			$tmpArr[] = '<a  href="'.$url.'/cid/'.$v['cid'].'">'.$v['cname'].'</a>';
    		}
    	}
    	$this->assign('sonCategory', $tmpArr);
    }
    
    /**
     * 设置地区的模板
     * */
    public function setLocality(){
    	$url = url_param_remove('lid',$this -> url);
    	$locality_model = new LocalityModel();
    	if(is_null($this -> lid)){
    		$topLocality = $locality_model -> getLocalityLevel(0);
    		$tmpArr = array();
    		$tmpArr[] =  '<a class="active" href="'.$url.'">全部</a>';
    		foreach ($topLocality as $v){
    			$tmpArr[] = '<a href="'.$url.'/lid/'.$v['lid'].'">'.$v['lname'].'</a>';
    		}
    		$this->assign('topLocality',$tmpArr);
    		return;
    	}
    	//有lid的情况
    	/**
    	 * 	1. lid 是顶级的地区id => pid = 0
    	 * 	2. lid 不是顶级地区的id
    	 */
    	$pid = $locality_model -> getLocalityPid($this -> lid);
    	$topLocality = $locality_model -> getLocalityLevel(0);
    	$tmpArr = array();
    	$tmpArr[] =  '<a href="'.$url.'">全部</a>';
    	foreach ($topLocality as $v){
    		if($pid == $v['lid'] || $this -> lid == $v['lid']){
    			$tmpArr[] = '<a  class="active" href="'.$url.'/lid/'.$v['lid'].'">'.$v['lname'].'</a>';
    		}else{
    			$tmpArr[] = '<a href="'.$url.'/lid/'.$v['lid'].'">'.$v['lname'].'</a>';
    		}
    	}
    	$this->assign('topLocality', $tmpArr);
    	//查出子地区
    	if($pid == 0){
    		$sonLocality = $locality_model -> getLocalityLevel($this -> lid);
    	}else{
    		$sonLocality = $locality_model -> getLocalityLevel($pid);
    	}
    	if(is_null($sonLocality)) return;
    	//组合子地区模板
    	$tmpArr = array();
    	if($pid == 0){
    		$tmpArr[] = '<a class="active"  href="'.$url.'/lid/'.$this -> lid.'">全部</a>';
    	}else{
    		$tmpArr[] = '<a  href="'.$url.'/lid/'.$pid.'">全部</a>';
    	}
    	foreach ($sonLocality as $v){
    		if($v['lid'] == $this -> lid){
    			$tmpArr[] = '<a class="active" href="'.$url.'/lid/'.$v['lid'].'">'.$v['lname'].'</a>';
    		}else{
    			$tmpArr[] = '<a  href="'.$url.'/lid/'.$v['lid'].'">'.$v['lname'].'</a>';
    		}
    	}
    	$this->assign('sonLocality', $tmpArr);
    }
    
    /**
     * 设置价格筛选模板
     */
    private function setPrice(){
    	$url = url_param_remove('price', $this->url);
    	$category_model = new CategoryModel();
    	$key = '';
    	if(is_null($this -> cid)){
    		$key = 'all';
    	}else{
    		$pid = $category_model -> getCategoryPid($this -> cid);
    		$key = $pid ? $pid : $this -> cid;
    	}
    	$prices = C('price');
    	$price = $prices[$key];
    	$tmpArr = array();
    	if(is_null($this -> price)){
    		$tmpArr[] = '<a class="active" href="'.$url.'">全部</a>';
    	}else{
    		$tmpArr[] = '<a  href="'.$url.'">全部</a>';
    	}
    	foreach ($price as $v){
    		if($this->price == $v[1]){
    			$tmpArr[] = '<a  class="active" href="'.$url.'/price/'.$v[1].'">'.$v[0].'</a>';
    		}else{
    			$tmpArr[] = '<a  href="'.$url.'/price/'.$v[1].'">'.$v[0].'</a>';
    		}
    	}
    	$this->assign('price', $tmpArr);
    }
    
    /**
     * 设置筛选条件
     */
    private function setSearchWhere(){
    	//组合分类条件
    	if(!is_null($this -> cid)){
    		$category_model = new CategoryModel();
    		$sonCids = $category_model -> getSonCategory($this -> cid);
    		foreach ($sonCids as $v){
    			$this -> goods_db -> cids[] = $v['cid'];
    		}
    	}
    	//组合地区条件
   		if(!is_null($this -> lid)){
    		$locality_model = new LocalityModel();
    		$sonLids = $locality_model -> getSonLocality($this -> lid);
    		foreach ($sonLids as $v){
    			$this -> goods_db -> lids[] = $v['lid'];
    		}
    	}
    	//组合价格条件
    	if(!is_null($this -> price)){
    		$priceArr = explode('-', $this -> price);
    		if(count($priceArr) > 1){
    			$this -> goods_db -> price = array(array('gt', $priceArr[0]), array('lt', $priceArr[1]), 'and');
    		}else{
    			$this -> goods_db -> price = array('gt', $priceArr[0]);
    		}
    	}
    }
    
    /**
     * 设置排序的模板
     */
    private function setOrderUrl(){
    	$url = url_param_remove('order',$this -> url);
    	$orderUrl = array();
    	//default 默认排序
    	$orderUrl['d'] = $url.'/order/t-desc';
    	//buy 销量降序
    	$orderUrl['b'] = $url.'/order/b-desc';
    	//price 价格降序
    	$orderUrl['p_d'] = $url.'/order/p-desc';
    	//price 价格升序
    	$orderUrl['p_a'] = $url.'/order/p-asc';
    	//begin_time 发表时间，降序
    	$orderUrl['t'] = $url.'/order/t-desc';
    	$this->assign('orderUrl', $orderUrl);
    }
    
    /**
     * 设置排序规则
     */
    private function setOrder(){
    	$order = '';
    	$arr = explode('-',$this -> order);
    	switch ($arr[0]){
    		case 'd':
    			$order = 'begin_time '.$arr[1];
    			break;
    		case 'b':
    			$order = 'buy '.$arr[1];
    			break;
    		case 'p':
    			$order = 'price '.$arr[1];
    			break;
    		case 't':
    			$order = 'begin_time '.$arr[1];
    			break;
    	}
		$this -> goods_db -> order = $order;
    }
}